package school.xauat.inscription_sys.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import school.xauat.inscription_sys.biz.BizCharactersService;
import school.xauat.inscription_sys.pojo.Bronzeware;
import school.xauat.inscription_sys.pojo.Characters;
import school.xauat.inscription_sys.pojo.Inscription;
import school.xauat.inscription_sys.pojo.vo.RespVo;
import school.xauat.inscription_sys.service.BronzewareService;
import school.xauat.inscription_sys.service.CharactersService;
import school.xauat.inscription_sys.service.InscriptionService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static school.xauat.inscription_sys.errorcode.ErrorCode.ERROR_UNKNOWN;

/**
 * @author ：zsy
 * @date ：Created 2022/4/26 21:30
 * @description：
 */
@Service
public class BizCharactersServiceImpl implements BizCharactersService {

    @Autowired
    CharactersService charactersService;

    @Autowired
    InscriptionService inscriptionService;

    @Autowired
    BronzewareService bronzewareService;

    class SearchResp {
        // 汉字相关
        public String charactersName;
        public String charactersInterpretation;
        // 铭文相关
        public String inscriptionInfo;
        public String inscriptionPicture;
        // 青铜器相关
        public String bwName;
        public String bwInfo;
        public String bwPicture;
    }

    @Override
    public RespVo searchByCharacters(String characters) {
        List<SearchResp> ans = null;
        try {
            Characters ch = charactersService.getOne(new QueryWrapper<Characters>().eq("name", characters));
            if (ch == null) {
                return RespVo.ok(String.format("%s, 不存在", characters));
            }
            List<Inscription> inscriptions = inscriptionService.mget(ch.getId());
            if (inscriptions.size() == 0) {
                return RespVo.ok(String.format("%s, 没有对应铭文信息", characters));
            }
            Map<Integer,Bronzeware> bwMap = new HashMap<>();
            for (Inscription inscription : inscriptions) {
                Bronzeware bw = bronzewareService.getById(inscription.getBronzewareId());
                if (bw != null) {
                    bwMap.put(inscription.getBronzewareId(), bw);
                }
            }
            ans = new ArrayList<>();
            for (Inscription inscription : inscriptions) {
                SearchResp searchResp = new SearchResp();
                searchResp.inscriptionInfo = inscription.getInfo();
                searchResp.charactersName = ch.getName();
                searchResp.charactersInterpretation = ch.getInterpretation();
                searchResp.inscriptionPicture = inscription.getPicture();
                if (bwMap.get(inscription.getBronzewareId()) == null) {
                    ans.add(searchResp);
                    continue;
                }
                searchResp.bwName = bwMap.get(inscription.getBronzewareId()).getName();
                searchResp.bwInfo = bwMap.get(inscription.getBronzewareId()).getInfo();
                searchResp.bwPicture = bwMap.get(inscription.getBronzewareId()).getPicture();
                ans.add(searchResp);
            }
        } catch (Exception e) {
            return RespVo.error(ERROR_UNKNOWN, e.getMessage());
        }

        return RespVo.obj(ans);
    }
}
